package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.MapConstant;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.AccountUserWork;
import com.tbit.main.pojo.Point;
import com.tbit.main.pojo.WorkOrderConfig;
import com.tbit.main.service.*;
import com.tbit.main.util.Assert;
import com.tbit.main.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 运维打卡
 *
 * @author LMY
 * @create 2021-03-22 15:22
 */
@RestController
@RequestMapping("/accountUserWork")
public class AccountUserWorkController {

    @Autowired
    private AccountUserWorkService accountUserWorkService;
    @Autowired
    private WorkOrderConfigService workOrderConfigService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private AccountUserService accountUserService;

    /**
     * 打卡
     *
     * @param token
     * @param lon
     * @param lat
     * @return
     */
    @RequestMapping("/clockIn")
    public JsonResult clockIn(String token, Integer accountId, Integer mapType, Double lon, Double lat) {
        Assert.notNull(lon, "Map.lon.nutNull");
        Assert.notNull(lat, "Map.lat.nutNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        AccountUser accountUserToken = tokenService.getAccount(token);

        /**获取是否存在*/
        LogUtil.info("打卡：" + accountUserToken.getName());
        accountUserWorkService.work(accountUserToken, accountId, mapType, lon, lat);
        return JsonResult.succ();
    }

    /**
     * 根据运维人员id查询
     *
     * @param token
     * @return
     */
    @RequestMapping("/getByAccountUserId")
    public JsonResult getByAccountUserId(String token, Integer mapType) {
        AccountUser accountUserToken = tokenService.getAccount(token);

        if (mapType == null) {
            mapType = MapConstant.MAP_GOOGLE;
        }

        AccountUserWork accountUserWork = accountUserWorkService.getByAccountUserId(accountUserToken.getAccountUserId());

        if (accountUserWork != null) {
            /**获取运维人员名称*/
            AccountUser accountUser = accountUserService.getById(accountUserWork.getAccountUserId());
            accountUserWork.setUserName(accountUser.getName());

            /**校准经纬度*/
            if (accountUserWork.getStartLat() != null) {
                Point point = new Point(accountUserWork.getStartLon(), accountUserWork.getStartLat());
                calibrateService.commonCalibrate(point, MapConstant.MAP_ORI, mapType);
                accountUserWork.setStartLatC(point.getX());
                accountUserWork.setStartLonC(point.getY());
            }
            if (accountUserWork.getEndLon() != null) {
                Point point = new Point(accountUserWork.getEndLon(), accountUserWork.getEndLat());
                calibrateService.commonCalibrate(point, MapConstant.MAP_ORI, mapType);
                accountUserWork.setEndLatC(point.getX());
                accountUserWork.setEndLonC(point.getY());
            }
        }
        return JsonResult.succ(accountUserWork);
    }

    @RequestMapping("/checkClockIn")
    public JsonResult<Boolean> checkClockIn(String token, Integer accountId) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        WorkOrderConfig workOrderConfig = workOrderConfigService.getByAccountId(accountId);

        // 没有开启强制上班打卡，直接返回 false
        if (workOrderConfig == null || !workOrderConfig.getClockInOrderSwitch()) {
            return JsonResult.succ(false);
        }

        AccountUser accountUser = tokenService.getAccount(token);

        // 如果为运维人员校验打卡信息
        return JsonResult.succ(accountUser.getSubRoleType().equals(2) // 运维人员
                && null == accountUserWorkService.getByAccountUserId(accountUser.getAccountUserId()));
    }
}
